package com.wontube.smr.bo.sites;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.sites.youtube.feed.YouTubeFeedsBO;
import com.wontube.smr.bo.sites.youtube.feed.YouTubeMovieFeedsBO;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.bo.MovieSelectDao;


/**
 * @author yuechao
 *
 */
public final class MovieSelectBO {
	private MovieSelectBO() {
	}
	
	public static Movie getMovie(final String movieId) {
		if (StringUtils.isNullOrEmpty(movieId)) {
			return null;
		}
		
		if (movieId.startsWith("yt")) {
			String videoId = movieId.substring("yt".length());
			try {
				return YouTubeFeedsBO.getSingle(videoId);
			} catch (Exception e) {
				return null;
			}
		}
		
		return MovieSelectDao.getMovie(movieId);
	}
	
	public static MovieItems selectHuluRecent(double dbFactor, PagenateInfo pageInfo) {
		return selectHuluRecent(null, dbFactor, pageInfo);
	}
	
	public static MovieItems selectHuluRecent(String genreName, double dbFactor, PagenateInfo pageInfo) {
		MovieItems items = new MovieItems(pageInfo);	
		if (dbFactor > 1 || dbFactor < 0) {
			dbFactor = 1;
		}
		
		// 优先展示资源库的内容
		int dbPageSize = (int)(pageInfo.getPageSize() * dbFactor);
		int dbSize = 0;
		int dbTotal = 0;
		if(dbPageSize > 0){
			PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
			items = MovieSelectDao.selectHuluRecentMovies(genreName, dbPageInfo);
			dbSize = items.getItems().size();
			dbTotal = items.getTotalCount();
		}
		
		// 展示YouTube内容
		int ybPageSize = pageInfo.getPageSize() - dbSize;		
		int ybSize = 0;
		int ybTotal = 0;
		if (ybPageSize > 0) {
			PagenateInfo feedsPageInfo = new PagenateInfo(pageInfo.getPageNo(), ybPageSize);
			MovieItems feedsItems = null;
			try {
				feedsItems = YouTubeMovieFeedsBO.selectRecentMovie(genreName, feedsPageInfo);
			} catch (Exception e) {				
			}
			
			if(feedsItems != null){
				ybSize = feedsItems.getItems().size();
				ybTotal = feedsItems.getTotalCount();
			}
			
			if(ybSize < ybPageSize && dbSize == dbPageSize){
				//重新db
				dbPageSize += ybPageSize - ybSize;
				PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
				items = MovieSelectDao.selectHuluRecentMovies(genreName, dbPageInfo);
				dbTotal = items.getTotalCount();
			}		
						
			if(feedsItems != null){
				for (Movie movie : feedsItems.getItems()) {
					items.getItems().add(movie);
				}
			}			
		}
		
		items.setTotalCount(dbTotal + ybTotal);
		
		return items;
	}
	
	public static MovieItems selectHuluPopular(double dbFactor, PagenateInfo pageInfo) {
		return selectHuluPopular(null, dbFactor, pageInfo);
	}
	
	public static MovieItems selectHuluPopular(String genreName, double dbFactor, PagenateInfo pageInfo) {
		MovieItems items = new MovieItems(pageInfo);	
		if (dbFactor > 1 || dbFactor < 0) {
			dbFactor = 1;
		}
		
		// 优先展示资源库的内容
		int dbPageSize = (int)(pageInfo.getPageSize() * dbFactor);
		int dbSize = 0;
		int dbTotal = 0;
		if(dbPageSize > 0){
			PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
			items = MovieSelectDao.selectHuluPopularMovies(genreName, dbPageInfo);
			dbSize = items.getItems().size();
			dbTotal = items.getTotalCount();
		}
		
		// 展示YouTube内容
		int ybPageSize = pageInfo.getPageSize() - dbSize;		
		int ybSize = 0;
		int ybTotal = 0;
		if (ybPageSize > 0) {
			PagenateInfo feedsPageInfo = new PagenateInfo(pageInfo.getPageNo(), ybPageSize);
			MovieItems feedsItems = null;
			try {
				feedsItems = YouTubeMovieFeedsBO.selectPopularMovie(genreName, feedsPageInfo);
			} catch (Exception e) {				
			}
			
			if(feedsItems != null){
				ybSize = feedsItems.getItems().size();
				ybTotal = feedsItems.getTotalCount();
			}
			
			if(ybSize < ybPageSize && dbSize == dbPageSize){
				//重新db
				dbPageSize += ybPageSize - ybSize;
				PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
				items = MovieSelectDao.selectHuluPopularMovies(genreName, dbPageInfo);
				dbTotal = items.getTotalCount();
			}		
						
			if(feedsItems != null){
				for (Movie movie : feedsItems.getItems()) {
					items.getItems().add(movie);
				}
			}			
		}
		
		items.setTotalCount(dbTotal + ybTotal);
		
		return items;
	}
	
	public static MovieItems selectHuluTopRated(double dbFactor, PagenateInfo pageInfo) {
		return selectHuluTopRated(dbFactor, null, pageInfo);
	}
	
	public static MovieItems selectHuluTopRated(double dbFactor, String genreName, PagenateInfo pageInfo) {
		MovieItems items = new MovieItems(pageInfo);	
		if (dbFactor > 1 || dbFactor < 0) {
			dbFactor = 1;
		}
		
		// 优先展示资源库的内容
		int dbPageSize = (int)(pageInfo.getPageSize() * dbFactor);
		int dbSize = 0;
		int dbTotal = 0;
		if(dbPageSize > 0){
			PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
			items = MovieSelectDao.selectHuluTopRatedMovies(genreName, dbPageInfo);
			dbSize = items.getItems().size();
			dbTotal = items.getTotalCount();
		}
		
		// 展示YouTube内容
		int ybPageSize = pageInfo.getPageSize() - dbSize;		
		int ybSize = 0;
		int ybTotal = 0;
		if (ybPageSize > 0) {
			PagenateInfo feedsPageInfo = new PagenateInfo(pageInfo.getPageNo(), ybPageSize);
			MovieItems feedsItems = null;
			try {
				feedsItems = YouTubeMovieFeedsBO.listTopRated(genreName, feedsPageInfo);
			} catch (Exception e) {				
			}
			
			if(feedsItems != null){
				ybSize = feedsItems.getItems().size();
				ybTotal = feedsItems.getTotalCount();
			}
			
			if(ybSize < ybPageSize && dbSize == dbPageSize){
				//重新db
				dbPageSize += ybPageSize - ybSize;
				PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
				items = MovieSelectDao.selectHuluTopRatedMovies(genreName, dbPageInfo);
				dbTotal = items.getTotalCount();
			}		
						
			if(feedsItems != null){
				for (Movie movie : feedsItems.getItems()) {
					items.getItems().add(movie);
				}
			}			
		}
		
		items.setTotalCount(dbTotal + ybTotal);
		
		return items;
	}	
	
	public static MovieItems searchHuluMovies(double dbFactor, String query, PagenateInfo pageInfo) {		
		MovieItems items = new MovieItems();		
		
		if (dbFactor > 1 || dbFactor < 0) {
			dbFactor = 1;
		}
		
		// 优先展示资源库的内容
		int dbPageSize = (int)(pageInfo.getPageSize() * dbFactor);
		int dbSize = 0;
		int dbTotal = 0;
		if(dbPageSize > 0){
			PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
			items = MovieSelectDao.search(query, Movie.TYPE_MOVIE, SiteConstants.WEBSITE_HULU, dbPageInfo);
			dbSize = items.getItems().size();
			dbTotal = items.getTotalCount();
		}
		
		int ybPageSize = pageInfo.getPageSize() - dbSize;		
		int ybSize = 0;
		int ybTotal = 0;
		if (ybPageSize > 0) {
			PagenateInfo ybPageInfo = new PagenateInfo(pageInfo.getPageNo(), ybPageSize);
			MovieItems feedsItems = null;
			try {
				feedsItems = YouTubeMovieFeedsBO.search(query, ybPageInfo);
			} catch (Exception e) {				
			}			
		
			if(feedsItems != null){
				ybSize = feedsItems.getItems().size();
				ybTotal = feedsItems.getTotalCount();
			}
			if(ybSize < ybPageSize && dbSize == dbPageSize){
				//重新db
				dbPageSize += ybPageSize - ybSize;
				PagenateInfo dbPageInfo = new PagenateInfo(pageInfo.getPageNo(), dbPageSize);
				items = MovieSelectDao.search(query, Movie.TYPE_MOVIE, dbPageInfo);			
			}
			
			if(feedsItems != null){
				for (Movie movie : feedsItems.getItems()) {
					items.getItems().add(movie);
				}
			}
		}
		
		items.setTotalCount(dbTotal + ybTotal);
		
		return items;
	}	
}
